package com.cloudeasy.server.config;

import com.cloudeasy.core.properties.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * Jwt配置
 */
@Configuration
public class JwtConfig {
    /**
     * {@link SecurityProperties}安全属性
     */
    private final SecurityProperties securityProperties;

    public JwtConfig(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }

    /**
     * 注册{@link JwtTokenStore}
     *
     * @return {@link JwtTokenStore}
     */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 注册{@link JwtAccessTokenConverter}
     * 应用安全属性中配置的JwtSigningKey
     *
     * @return {@link JwtAccessTokenConverter}
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey(securityProperties.getOauth2().getJwtSigningKey());
        return jwtAccessTokenConverter;
    }
}
